import { constantRoutes } from '@/router'
import { userMenus } from '@/api/user'
import Layout from '@/layout/index'
import { setRouter, getRouter } from '@/utils/localStorageData'

const permission = {
  state: {
    routes: [],
    addRoutes: []
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)

    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
      state.sidebarRouters = routes
    }
  },
  actions: {
    checkRoutes({commit},router){
      return new Promise(resolve => {
        if (router.length < 20){
          commit('SET_ROUTES', getRouter())
          resolve(getRouter())
        }else {
          resolve([])
        }
      })

    },
    // 生成路由
    GenerateRoutes({ commit }) {
      return new Promise(resolve => {
        // 向后端请求路由数据
        userMenus().then(res => {
          // let a = 0
          res.data.forEach((item, index) => {
            // console.log(item.component)
            const str = item.component
            Object.assign(item, { str: str })
            Object.assign(item, { component: 'Layout' })
            const obj = {
              icon: item.icon,
              title: item.menu_name,
              cache:true,
            }
            Object.assign(item, { meta: obj })
            if (item.children) {
              Object.assign(item, { pid: 1 })
              item.children.forEach(itm => {
                const obj = {
                  icon: itm.icon,
                  title: itm.menu_name,
                  cache:true,
                }
                Object.assign(itm, { meta: obj })
              })
            } else {
              if (item.menu_type === 'F') {
                // res.data.splice(index, 1)
              } else {
                Object.assign(item, { pid: 0 })
                const o = {
                  component: item.str,
                  icon: item.icon,
                  menu_code: item.code,
                  menu_name: item.menu_name,
                  menu_status: item.menu_status,
                  menu_type: item.menu_type,
                  meta: item.meta,
                  parent_id: item.parent_id,
                  path: item.path,
                  pid: item.pid,
                  sort: item.sort,
                  str: item.str,
                  _id: item.str
                }
                const children = []
                children.push(o)
                Object.assign(item, { children: children })
                Object.assign(item, { component: 'Layout' })
              }
            }
          })
          console.log(res.data);
          const accessedRoutes = filterAsyncRouter(res.data)
          accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
          setRouter(accessedRoutes)
          commit('SET_ROUTES', accessedRoutes)

          resolve(accessedRoutes)
        })
      })
    }
  }
}
// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      if (route.component === 'Layout') {
        route.component = Layout
        // if (route.pid == 1) {
        //     route.component = Layout
        // } else {
        //     route.component = loadView(route.str)
        // }
      } else {
        route.component = loadView(route.component)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })
}
export const loadView = (view) => { // 路由懒加载
  return (resolve) => {
    require([`@/views/${view}`], resolve)
  }
}
export default permission
